<?php
/**
 * Created by PhpStorm.
 * User: yys
 * Date: 2016/11/26
 * Time: 11:57
 */

namespace App\Services\ColourService;


use App\Services\ImgServices\ImgTool;
use Illuminate\Support\Facades\DB;

class ColourTool
{
    /****
     * 获取颜色简要信息
     * @param $color_id
     * @return null
     */
    public static function getColourSimpleInfoByColourId($colour_id){
        if(empty($colour_id)||!is_int($colour_id)) {
            return null;
        }
        $colour= DB::table('colours')->where(array('state'=>1,'id'=>$colour_id))
            ->select('colour_name','id as colour_id','colour_code','colour_image')
            ->first();
        if(!empty($colour)){
            $colour->colour_image=ImgTool::getImgUrlByImgPath($colour->colour_image);
        }
        return $colour;
    }

    /***
     * 获取颜色数组
     * @param $colour_ids
     * @return array
     */
    public static function getColoursByColourIds($colour_ids){
        if(empty($colour_ids)||!is_array($colour_ids)) {
            return array();
        }
        $colours= DB::table('colours')->where(array('state'=>1))
            ->whereIn('id',$colour_ids)
            ->select('colour_name','id as colour_id','colour_code','colour_image')
            ->get()
            ->toArray();
        foreach ($colours as $colour){
            $colour->colour_image=ImgTool::getImgUrlByImgPath($colour->colour_image);
        }
        return empty($colours)?array():$colours;
    }
    public static function getColourByColourId($colour_id){
        if(empty($colour_id)) {
            return null;
        }
        $colour= DB::table('colours')->where(array('state'=>1))
            ->where('id',$colour_id)
            ->select('colour_name','id as colour_id','colour_code','colour_image')
            ->first();
        if(!empty($colour)){
            $colour->colour_image=ImgTool::getImgUrlByImgPath($colour->colour_image);
        }
        return $colour;
    }
    /***
     * @param $colour_ids
     * @return array
     */
    public static function getColoursWhereNotInColourIds($colour_ids){
        if(!is_array($colour_ids)) {
            return array();
        }
        $colours= DB::table('colours')->where(array('state'=>1))
            ->whereNotIn('id',$colour_ids)
            ->select('colour_name','id as colour_id','colour_code','colour_image')
            ->get()
            ->toArray();
        foreach ($colours as $colour){
            $colour->colour_image=ImgTool::getImgUrlByImgPath($colour->colour_image);
        }
        return empty($colours)?array():$colours;
    }
    /***
     * 颜色ID是否是可用的
     * @param $colour_id
     * @return bool
     */
    public static function colourExistByColourId($colour_id){
        if(empty($colour_id)){
            return false;
        }
        $num=DB::table('colours')->where(array('id'=>$colour_id,'state'=>1))
            ->count();
        return ($num==0)?false:true;
    }

    /***
     * 获取颜色ID对应的颜色名
     * @param $colour_id
     * @return string
     */
    public static function getColourNameByColourId($colour_id){
        if(empty($colour_id)){
            return '';
        }
        $colour_name=DB::table('colours')->where(array('id'=>$colour_id,'state'=>1))
            ->value('colour_name');
        return empty($colour_name)?'':$colour_name;
    }

    /***
     * 获取颜色名对应的颜色Id
     * @param $colour_name
     * @return int
     */
    public static function getColourIdByColourName($colour_name){
        if(empty($colour_name)){
            return 0;
        }
        $colour_id=DB::table('colours')->where(array('colour_name'=>$colour_name,'state'=>1))
            ->value('id');
        return empty($colour_id)?0:$colour_id;
    }
}